/****************************************************************************
 * Chariott - Continuous Integration Testing Tool
 * Zaid Mohsin
 * The University of York
 * 2012/2013
 * MSc in Information Technology
 * All rights reserved.
 ****************************************************************************/
package chariott.main;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jdom2.JDOMException;

/**
 * Main Entry point class for ChARIOTT. Referenced by META-INF in chariott.jar
 * 
 * @author Zaid
 * 
 */
public class Main {

	public final static Logger logger = Logger.getLogger(Main.class);

	/**
	 * main entry method for ChARIOTT to be packaged in JAR file
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		PropertyConfigurator.configure("logConfig.txt");

		// launch main application for Chariott if -launch is passed as an
		// argument or chariott is launched with no arguments.
		if (args.length == 0 || args[0].equals("-launch")) {

			logger.info("Initiating ChARRIOTT Launcher...");
			launchChariott();
		}
		// launch the test report aggregation tool
		else if (args[0].equals("-aggregate")) {

			// if no test results folder is specified
			if (args.length <= 1) {
				System.out
						.println("Please specifiy a valid test results folder generated by ant-junit");
				System.exit(0);
			} else {
				File testFolder = new File(args[1]);
				// check the passed folder is a proper directory
				if (!testFolder.isDirectory()) {
					System.out
							.println("Please specifiy a correct test results folder");
					System.exit(0);
				}

				launchTestAggregator(testFolder);
			}
		}
		// checks if time estimator is passed to launch it
		else if (args[0].equals("-estimate")) {

			if (args.length <= 1) {
				System.out
						.println("Please Specify a tests configuration XML file to Write the new estimates to");
				System.exit(0);
			} else
				// launch estimator
				launchTestEstimator(new File(args[1]));
		} else if (args[0].equals("-reset")) {

			// clear the test results table
			Main.logger.info("Resetting ChARIOTT test results database...");

			TestsDBConnector myConnector = new TestsDBConnector();
			myConnector.resetTable();

		} else {
			System.out
					.println("Please use a correct option either -launch, -aggregate, or -estimate to run Chariott");
			System.exit(0);
		}

	}

	/**
	 * Launches the main chariott tool to configure the tool, run the cloud
	 * nodes, generate the scripts, and execute the script.
	 * 
	 */
	public static void launchChariott() {
		new ChariottLauncher();
	}

	/**
	 * launch the test aggregator to fetch the results folder and generate an
	 * html report with xml JUnit test suite for all the tests
	 * 
	 * @param testResultsFolder
	 *            the test results folder
	 */
	public static void launchTestAggregator(File testResultsFolder) {

		logger.info("Initializing ChARIOTT Aggregator...");
		TestAggregator myAggregator = new TestAggregator(testResultsFolder);
		// test aggregator will aggregate the test results and produce an html
		// report
		logger.info("Generating report from: " + testResultsFolder.getName()
				+ " to: ~/testreport");
		myAggregator.generateReport();

		// add the results from the test cases to the database.
		logger.info("Adding the new results to the Database");
		try {
			myAggregator.addTests();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * launches the test estimator to write the new values gathered from the
	 * test results into the testsToRun.txt file
	 * 
	 */
	public static void launchTestEstimator(File testsConfigFile) {
		logger.info("Initiating ChARRIOTT Test Estimator...");
		new TestTimeEstimator(testsConfigFile);
	}

}
